package cn.ydt.ydt.community.webapi.service.impl;

import cn.ydt.ydt.common.exception.YdtServiceException;
import cn.ydt.ydt.common.resful.JsonPage;
import cn.ydt.ydt.common.resful.StatusCode;
import cn.ydt.ydt.community.service.NoticeService;
import cn.ydt.ydt.community.webapi.mapper.NoticeMapper;
import cn.ydt.ydt.community.webapi.utils.SecurityContextUtils;
import cn.ydt.ydt.pojo.community.dto.NoticeAddDTO;
import cn.ydt.ydt.pojo.community.dto.NoticeDeleteDTO;
import cn.ydt.ydt.pojo.community.dto.NoticeListDTO;
import cn.ydt.ydt.pojo.community.dto.NoticeUpdateDTO;
import cn.ydt.ydt.pojo.community.model.Notice;
import cn.ydt.ydt.pojo.community.vo.NoticeAddVO;
import cn.ydt.ydt.pojo.community.vo.NoticeListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void addNotice(NoticeAddDTO noticeAddDTO) {
        log.info("控制层调用新增通知--开始");
        Long communityId = SecurityContextUtils.getNetCommunityId();
        Notice notice = new Notice();
        notice.setNoticeContent(noticeAddDTO.getNoticeContent());
        LocalDateTime dateTime = LocalDateTime.now();
        notice.setIsDeleted((byte)0);
        notice.setModifyTime(dateTime);
        notice.setCreateTime(dateTime);
        notice.setCommunityId(communityId);

        //新增入库
        noticeMapper.insertNotice(notice);

        log.info("控制层调用新增通知--结束");
    }

    @Override
    public void updateNoticeByNoticeIdAndCommunityId(NoticeUpdateDTO noticeUpdateDTO) {
        log.info("控制层调用修改通知--开始");

        noticeUpdateDTO.setNoticeId(1L);
        noticeUpdateDTO.setCommunityId(1L);
        noticeUpdateDTO.setNoticeContent("呔，何人敢出门！");
        noticeUpdateDTO.setModifyTime(new Date());
        //新增入库
        noticeMapper.updateNoticeByNoticeIdAndCommunityId(noticeUpdateDTO);

        log.info("控制层调用修改通知--结束");
    }

    @Override
    public List<NoticeListVO> selectNoticeByCommunityId() {
        log.info("控制层调用查询通知--开始");

        Long communityId = SecurityContextUtils.getNetCommunityId();
        List<NoticeListVO> list = noticeMapper.selectNoticeByCommunityId(communityId);

        log.info("控制层调用查询通知--结束");
        return list;
    }

    @Override
    public List<NoticeListVO> selectNoticeByCommunityIdUser() {
        Long communityId = SecurityContextUtils.getUserInfo().getCommunityId();
        log.info("社区id={}",communityId);
        List<NoticeListVO> list = noticeMapper.selectNoticeByCommunityIdUser(communityId);
        return list;
    }

    @Override
    public JsonPage<NoticeListVO> selectNoticePageByCommunityId(NoticeListDTO noticeListDTO) {
        log.info("控制层调用分页查询通知--开始");

        // 设置社区id
        noticeListDTO.setCommunityId(1L);
        // 设置第几页
        noticeListDTO.setPage(1);
        // 设置条数
        noticeListDTO.setPageSize(3);
        // 设置分页条件
        PageHelper.startPage(noticeListDTO.getPage(), noticeListDTO.getPageSize());
        //执行查询
        List<NoticeListVO> list = noticeMapper.selectNoticePageByCommunityId(noticeListDTO);

        log.info("控制层调用分页查询通知--结束");

        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public void deleteNoticeByNoticeIdAndCommunityId(Long  noticeId) {
        log.info("控制层调用删除通知--开始");
        if (noticeId == null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"该通知已被删除");
        }
        noticeMapper.deleteNoticeByNoticeIdAndCommunityId(noticeId);

        log.info("控制层调用修删除通知--结束");
    }


}
